---
title: "CREDI_FATHER_WEIGHTED_R"
date: "2024-04-03"
output: pdf_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```


## R Markdown
```{r}
library(tidyverse)
library(lme4)
library(lmerTest)
library(haven)

```

## Load and merge data

```{r}
dat <- read_dta("/Users/dujamichael/Library/CloudStorage/Box-Box/ECDEC - Shared Files/04_Projects/Play to Learn/HPL 2-4 Impact Evaluation, home-based centers/6_RCT/Data/Quantitative/Baseline/Resources/data/Analysis datasets/imp_anaysis_data.dta")
credi_f <- read_dta("/Users/dujamichael/Library/CloudStorage/Box-Box/ECDEC - Shared Files/04_Projects/Play to Learn/HPL 2-4 Impact Evaluation, home-based centers/6_RCT/Data/Quantitative/Baseline/Resources/data/Analysis datasets/final/CREDI_Father_data_all_ages.dta")
colnames(credi_f)[1] <- "hhid_final"

datf <- left_join(credi_f,dat, by="hhid_final")
```


## Overall sample
```{r}
## drop outliers
datf <- datf[!(datf$hhid_final %in% c("H-0279-C-70207", "H-0288-C-73355", "H-0455-C-77043",
                              "H-0428-C-70328","H-0189-C-76576","H-0402-C-71403",
                              "H-0179-C-75382","H-0376-C-77518","H-0179-C-75382",
                              "H-0177-C-69709")), ]

## WEIGHTS
datf <- datf %>% mutate(weight_SEM=1/((SEM_SE)^2))


##THIS IS ADJUSTED TO ONlY HAVE 1 CLUSTERING LEVEL DUE TO CONVERGENCE ISSUES
fitSEMw <- lmer(data=datf, SEM ~ tx + camp+ 
                 `_1_f_age`+ `_1_m_age`+ as.factor(fb_q1_edu) +as.factor(fb_grade)+as.factor(fb_q4_edu) +
                 `_1_child_fem` +AGE+ 
                 as.factor(`_1_mb_q1_hunger`) +as.factor(`_1_mb_q2_hunger`)+ `_1_mb_q4_pregnant` + `_1_mb_child_count`+ 
                 as.factor(`_1_mb_q2_fin`) + `_1_hh_res` + `_1_hh_qua`  +
                 camp:`_1_f_age` +camp:`_1_m_age` +camp:fb_grade +camp:as.factor(fb_q4_edu) +
                 camp:`_1_child_fem` +camp:AGE +
                 camp:`_1_mb_q2_hunger` +camp:`_1_mb_q4_pregnant` +
                 camp:`_1_mb_q2_fin` +camp:`_1_hh_res` +
                 (1|mvid), REML=FALSE, weights = weight_SEM)
summary(fitSEMw)

```

## CAMP sample

```{r}

datfc <- datf %>% filter(camp==1)

# weighted
##THIS IS ADJUSTED TO ONlY HAVE 1 CLUSTERING LEVEL DUE TO CONVERGENCE ISSUES

fitSELcw <- lmer(data=datfc, SEM ~ tx + 
                  `_1_f_age`+ `_1_m_age`+ as.factor(fb_q1_edu) +as.factor(fb_grade)+as.factor(fb_q4_edu) +
                  `_1_child_fem` +AGE+ 
                  as.factor(`_1_mb_q1_hunger`) +as.factor(`_1_mb_q2_hunger`)+ `_1_mb_q4_pregnant` + `_1_mb_child_count`+ 
                  as.factor(`_1_mb_q2_fin`) + `_1_hh_res` + `_1_hh_qua`  +
                  (1|mvid), REML=FALSE, weights = weight_SEM)

summary(fitSELcw)

```



## HOST sample

```{r}

##HOST
datfh <- datf %>% filter(camp==0)

# weighted
##THIS IS ADJUSTED TO ONlY HAVE 1 CLUSTERING LEVEL DUE TO CONVERGENCE ISSUES

fitSELhw <- lmer(data=datfh, SEM ~ tx + camp+ 
                   `_1_f_age`+ `_1_m_age`+ as.factor(fb_q1_edu) +as.factor(fb_grade)+as.factor(fb_q4_edu) +
                   `_1_child_fem` +AGE+ 
                   as.factor(`_1_mb_q1_hunger`) +as.factor(`_1_mb_q2_hunger`)+ `_1_mb_q4_pregnant` + `_1_mb_child_count`+ 
                   as.factor(`_1_mb_q2_fin`) + `_1_hh_res` + `_1_hh_qua`  +
                   (1|mvid), REML=FALSE, weights = weight_SEM)
summary(fitSELhw)
```

# Overall TABLE

```{r}
#overall cog
#overall sel
overall_sel_w_est=coef(summary(as(fitSEMw,"lmerModLmerTest")))["tx","Estimate"]
overall_sel_w_p  =coef(summary(as(fitSEMw,"lmerModLmerTest")))["tx","Pr(>|t|)"]

overll_w_coeffs=c(overall_sel_w_est)
overll_w_ps=c(overall_sel_w_p)
overall_table=cbind(overll_w_coeffs,overll_w_ps)
rownames(overall_table) <- c("SEL")
colnames(overall_table) <- c("Coefficient", "p-value")
overall_table <- round(overall_table, 3)
knitr::kable(overall_table)
```

# CAMP TABLE
```{r}

#overall sel
selc_est=coef(summary(as(fitSELc,"lmerModLmerTest")))["tx","Estimate"]
selc_p  =coef(summary(as(fitSELc,"lmerModLmerTest")))["tx","Pr(>|t|)"]

selc_w_est=coef(summary(as(fitSELcw,"lmerModLmerTest")))["tx","Estimate"]
selc_w_p  =coef(summary(as(fitSELcw,"lmerModLmerTest")))["tx","Pr(>|t|)"]

cw_coeffs=c(selc_w_est)
cw_ps=c(selc_w_p)
table_c=cbind(cw_coeffs,cw_ps)
rownames(table_c) <- c("SEL")
colnames(table_c) <- c("Coefficient", "p-value")
table_c <- round(table_c, 3)
knitr::kable(table_c)
```



# HOST TABLE
```{r}

# sel
selh_w_est=coef(summary(as(fitSELhw,"lmerModLmerTest")))["tx","Estimate"]
selh_w_p  =coef(summary(as(fitSELhw,"lmerModLmerTest")))["tx","Pr(>|t|)"]

hw_coeffs=c(selh_w_est)
hw_ps=c(selh_w_p)
table_h=cbind(hw_coeffs,hw_ps)
rownames(table_h) <- c("SEL")
colnames(table_h) <- c("Coefficient", "p-value")
table_h <- table_h(table_h, 3)
knitr::kable(table_h)
```